#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+100;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn][maxn];
LL ma[maxn][maxn];
bool vis[maxn][maxn];
LL n,m,sum=0;
LL dx[4]={1,0,0,-1};
LL dy[4]={0,1,-1,0};
struct P{
    LL x,y;
};
queue<P>que;
void bfs(LL x,LL y){
     if(vis[x][y]) return;
     vis[x][y]=true;
     que.push({x,y});
     while(!que.empty()){
        P now=que.front();que.pop();
        if(ma[now.x][now.y]==0) sum++;
        for(LL i=0;i<4;i++){
            LL nx=now.x+dx[i];LL ny=now.y+dy[i];
            if(nx<1||nx>n||ny<1||ny>m) continue;
            if(vis[nx][ny]||ma[nx][ny]==1) continue;
            vis[nx][ny]=1;
            que.push({nx,ny});
        }
     }
}
int main(void){
   	cin.tie(0);std::ios::sync_with_stdio(false);
    cin>>n>>m;
    vector<P>v;
    for(LL i=1;i<=n;i++){
        for(LL j=1;j<=m;j++){
            cin>>str[i][j];
            if(str[i][j]=='1') ma[i][j]=1;
            if(str[i][j]=='0') ma[i][j]=0;
            if(str[i][j]=='.') ma[i][j]=3;
            if(i==1||j==1||i==n||j==m) v.push_back({i,j});
        }
    }
    for(auto i:v){
        LL x=i.x;LL y=i.y;
        if(ma[x][y]==1) continue;
        bfs(x,y);
    }
    cout<<sum<<"\n";
   	return 0;
}
